<template>
  <div>
    <!-- 出院管理 -->
    <!-- 搜索 -->
    <el-card class="card">
      <el-form ref="ruleFormRef" :inline="true" class="demo-form-inline" :model="params">
        <el-form-item label="老人姓名：" prop="name">
          <el-input v-model="params.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="身份证号：" prop="idCard">
          <el-input v-model="params.idCard" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态：" prop="state">
          <el-select v-model="params.state!" clearable style="width: 200px">
            <el-option v-for="item in stateList" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="card">
      <ELTable v-bind="tableConfig" ref="dischargeTable">
        <template #elderlyGender="{ scope }">
          {{ scope.row.elderlyGender == "1" ? "男" : "女" }}
        </template>
        <template #addTime="{ scope }">
          {{ dayJs(scope.row.addTime).format("YYYY-MM-DD HH:mm") }}
        </template>
        <template #state="{ scope }">
          {{ scope.row.state == 0 ? "待出院" : "已出院" }}
        </template>
        <template #operate="{ scope, deleteHandle }">
          <el-button type="primary" :icon="Edit" text @click="update(scope.row.id,scope.row.elderlyName)">编辑</el-button>
          <el-button type="danger" :icon="Delete" text @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
        <template #btns="{ deleteAll }">
          <el-button type="success" :icon="Plus" @click="add">新增</el-button>
          <el-button :icon="Delete" type="danger" @click="deleteAll">批量删除</el-button>
        </template>
      </ELTable>
    </el-card>
    <AddDischarge v-if="dialogVisible" :id="updateId" :currentName="currentName" @close="closeHandle" />
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ListParams } from "../../api/market/discharge/type";
import { getDischargeList, delDischarge, delMany } from "../../api/market/discharge/discharge";
import type { FormInstance } from "element-plus";
import ELTable, { TableData } from "../../components/ELTable.vue";
import dayJs from "dayjs";
import { Delete, Edit, Plus, Search, Refresh } from "@element-plus/icons-vue";
import AddDischarge from "../../components/market/discharge/AddDischarge.vue";

const dischargeTable = ref();
const ruleFormRef = ref<FormInstance>();
//查询参数
const params = reactive<ListParams>({});

//状态
const stateList = ref([
  { id: 0, name: "待出院" },
  { id: 1, name: "已出院" },
]);

//查询
const search = () => {
  dischargeTable.value.getData(params);
};
//重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

//表格数据
const tableConfig = reactive<TableData>({
  loadData: getDischargeList,
  delete: delDischarge,
  deleteAll: delMany,
  columns: [
    {
      prop: "id",
      label: "序号",
    },

    {
      prop: "elderlyName",
      label: "老人姓名",
    },
    {
      prop: "elderlyGender",
      label: "性别",
      slot: true,
    },
    {
      prop: "elderlyIdCard",
      label: "身份证号",
    },
    {
      prop: "begName",
      label: "原床位",
    },
    {
      prop: "begName",
      label: "变更后床位",
    },
    {
      prop: "addAccountName",
      label: "申请人",
    },
    {
      prop: "addTime",
      label: "申请日期",
      slot: true,
    },
    {
      prop: "state",
      label: "状态",
      slot: true,
    },
    {
      prop: "operate",
      label: "操作",
      slot: true,
      attrs: {
        width: 200,
      },
    },
  ],
});

//新增dialog
let dialogVisible = ref(false);
//添加
const add = () => {
  updateId.value=0;
  currentName.value="";
  dialogVisible.value = true;
};
//修改id
const updateId = ref(0);
const currentName=ref("");
//修改
const update = (id: number,name:string) => {
  // console.log("11修改name",name);
  currentName.value=name;
  updateId.value = id;
  dialogVisible.value = true;
};
//关闭新增dialog
const closeHandle = (data: boolean) => {
  dialogVisible.value = false;
  if (data) {
    //表格刷新
    dischargeTable.value.getData();
  }
};
</script>

<style scoped>
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>
